package cn.hu.order.service.impl;

import cn.hu.basic.commonenum.ErrorCode;
import cn.hu.basic.exception.CustomException;
import cn.hu.basic.util.CodeGenerateUtils;
import cn.hu.basic.util.CurrentLoginInfoUtil;
import cn.hu.order.domain.OrderAddress;
import cn.hu.order.domain.OrderAdopt;
import cn.hu.order.mapper.OrderAddressMapper;
import cn.hu.order.mapper.OrderAdoptMapper;
import cn.hu.order.service.IOrderAdoptService;
import cn.hu.basic.service.impl.BaseServiceImpl;
import cn.hu.pet.domain.Pet;
import cn.hu.pet.mapper.PetMapper;
import cn.hu.user.domain.Logininfo;
import cn.hu.user.domain.User;
import cn.hu.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Hu
 * @since 2022-12-31
 */
@Service
public class OrderAdoptServiceImpl extends BaseServiceImpl<OrderAdopt> implements IOrderAdoptService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderAdoptMapper orderAdoptMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private PetMapper petMapper;
    /*
    下单
     */
    @Override
    @Transactional
    public void placeOrder(Pet pet, HttpServletRequest request) throws Exception {
        //上架的宠物才能下单
        //如果下架了就抛异常
        if (pet.getState() == 0){
            throw new CustomException(ErrorCode.PET_SOLD_OUT.getCode());
        }
        OrderAdopt orderAdopt = new OrderAdopt();
        //设置摘要为对宠物名字的领养订单
        orderAdopt.setDigest("对"+pet.getName()+"的领养订单！");
        //状态设置为待支付
        orderAdopt.setState(0);
        orderAdopt.setPrice(pet.getSaleprice());
        //订单编号
        orderAdopt.setOrderSn(CodeGenerateUtils.generateOrderSn(pet.getId()));
        //支付订单编号暂时为空,支付时间和确认时间也为空
        orderAdopt.setPetId(pet.getId());
        Logininfo lf = CurrentLoginInfoUtil.currentLoginInfo(request);
        User user = userMapper.findByLoginInfoId(lf.getId());
        orderAdopt.setUserId(user.getId());
        orderAdopt.setShopId(pet.getShopId());
        orderAdopt.setServiceMethod(0);
        orderAdoptMapper.add(orderAdopt);
        //address表
        OrderAddress orderAddress = new OrderAddress();
        orderAddress.setCreateTime(new Date());
        orderAddress.setOrderId(orderAdopt.getId());
        orderAddress.setOrderSn(orderAdopt.getOrderSn());
        orderAddress.setContacts("艾迪");
        orderAddress.setAreaCode("湖北省 武汉市 洪山区");
        orderAddress.setAddress("雄楚大道666号(中南财经政法大学)");
        orderAddress.setFullAddress("成都市高新区府城大道西段399号天府新谷1号楼6F");
        orderAddress.setPhone("15888888888");
        orderAddress.setPhoneBack("15888888888");
        orderAddress.setPostCode("637500");
        orderAddress.setEmail("yhptest@itsource.cn");
        orderAddressMapper.add(orderAddress);
        //被领养后pet表的状态设置为0下架，下架时间,shopid置为空，userId设置为用户
        pet.setState(0);
        pet.setOffsaletime(new Date());
        pet.setShopId(null);
        pet.setUserId(user.getId());
        petMapper.update(pet);
    }
}
